package cn.shutdown.redission;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @Auther linmengmeng
 * @Date 2021-09-01 15:37
 */
@Slf4j
@RestController
@RequestMapping("/tourist")
public class TestRedissonLockController {

    private static String FORMAT_LOCKKEY = "testLockKey:%s";//分布式锁的key

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private Redisson redisson;

    @RequestMapping("/testLock11")
    public ApiResult<Boolean> testLock11() {
        String lockKey = String.format(FORMAT_LOCKKEY, 3);
        log.info("-------lockKey:{}", lockKey);
        RLock lock = redisson.getLock(lockKey);
        log.info("-------创建锁之后 isLocked-1:{}", lock.isLocked());
        //        try {
        //            Thread.sleep(10000);
        //        } catch (InterruptedException e) {
        //            e.printStackTrace();
        //        }
        Future<Boolean> res = lock.tryLockAsync(10, 30, TimeUnit.SECONDS);
        log.info("-------tryLockAsync 后 isLocked-2:{}", lock.isLocked());
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (lock.isLocked()) {
            log.info("-------10秒 后----isLocked-3:{}", lock.isLocked());
            //throw new BusinessException("测试获取锁后发生异常");
        }
        if (lock.isHeldByCurrentThread()) {
            log.info("-------isHeldByCurrentThread:{}", lock.isHeldByCurrentThread());
        }
        boolean result = false;
        try {
            result = res.get();
            log.info("-------result:" + result);
            if (result) {
                Thread.sleep(10000);
                if (lock.isHeldByCurrentThread()) {
                    log.info("-------isHeldByCurrentThread:{}", lock.isHeldByCurrentThread());
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } finally {
            log.info("-------666666-------unlock-isLocked:{}", lock.isLocked());
            if (lock.isLocked()) {
                log.info("-------88888888-------解锁解锁:{}", lock.isLocked());
                lock.unlock();
            }
        }
        log.info("res.get:{}", result);
        return ApiResult.ok(lock.isLocked());
    }

    @RequestMapping("/testLock12")
    public ApiResult testLock12() {
        String lockKey = String.format(FORMAT_LOCKKEY, 3);
        log.info("====================lockKey:{}", lockKey);
        RLock lock = redisson.getLock(lockKey);
        log.info("====================isLocked-1:{}", lock.isLocked());
        Future<Boolean> res = lock.tryLockAsync(5, 2, TimeUnit.SECONDS);
        boolean locked = lock.isLocked();
        log.info("====================isLocked-2:{}", locked);
        if (locked) {
            if (lock.isHeldByCurrentThread()) {
                log.info("====================锁住了，是我的锁");
            } else {
                log.info("====================锁住了，不是我的锁");
            }
        }
        Boolean getLock = null;
        log.info("====================getLock-2:{}", getLock);
        return ApiResult.ok(locked);
    }

}
